﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class ProvinceMaster : MasterBase
    {
        public ResultInfo GetAllItems(string key)
        {
            var items = GetInstance<ProvinceService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_BASE), key);
            return new ResultInfo(ResultValue.Succeed, items.ToArray());
        }

        public ResultInfo Update(string account, string password, ProvinceInfo entity, EntityOperation operation)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<ProvinceService>().Update(ConnectionString(DatabaseKeys.CORINA_BASE), entity, operation);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            return new ResultInfo(resultValue);
        }

        public ResultInfo GetNewEntity(string account, string password)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result =new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                string code = GetInstance<ProvinceService>().GetCode(ConnectionString(DatabaseKeys.CORINA_BASE));

                ProvinceInfo entity = new ProvinceInfo() { Code = code };

                result.Entities.Add(entity);
            }

            return result;
        }
    }
}
